package cn.jy.careuplusdoctor.utils;

import java.io.IOException;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtils {

	private static JsonUtils instance;
	private ObjectMapper mJsonMapper;
	private JsonUtils() {
		initJsonMapper();
	}
	
	public static JsonUtils getInstance() {
		if (instance ==null) {
			instance = new JsonUtils();
		}
		return instance;
	}
	public ObjectMapper getJsonMapper()
	{
		initJsonMapper();
		return mJsonMapper;
		
	}
	
	private void initJsonMapper()
	{
		if (mJsonMapper == null) {
			mJsonMapper = new ObjectMapper();
			mJsonMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
			mJsonMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		}
	}
	
	public Object readValue(String json,Class<?> clazz)
	{
		try {
			return mJsonMapper.readValue(json, clazz);
		} catch (JsonParseException e) {
			e.printStackTrace();
			return null;
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}

